package com.gy.mbaselibrary.glidetransform;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.TransformationUtils;

import java.security.MessageDigest;


/**
 * 创建者：高业
 * 邮箱：15234150168@163.com
 * 时间：2018/2/6
 * 描述：解决圆角和centerCrop冲突
 * 修改人：
 * 修改时间：2018/2/6
 * 修改备注：
 */
public class GlideRoundTransform extends BitmapTransformation {

    private static float radius = 5f;

    private TransType type;
    public enum TransType{
        TYPE_ALL,TYPE_BOTTOM,TYPE_TOP,TYPE_TOP_LEFT,TYPE_RIGHT
    }


    public GlideRoundTransform() {
        this(5,TransType.TYPE_ALL);
    }

    public GlideRoundTransform(int dp) {
        this(dp,TransType.TYPE_ALL);
    }

    public GlideRoundTransform(int dp,TransType type) {
        super();
        this.type = type;
        this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
    }

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
        return roundCrop(pool, bitmap);
    }

    private Bitmap roundCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;

        Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
        canvas.drawRoundRect(rectF, radius, radius, paint);
        if (type == TransType.TYPE_TOP || type == TransType.TYPE_BOTTOM || type == TransType.TYPE_TOP_LEFT){
            int roundPx = (int) radius;
            //把不需要的圆角去掉
            if (type == TransType.TYPE_TOP){
                clipBottomLeft(canvas,paint,roundPx,source.getWidth(),source.getHeight());
                clipBottomRight(canvas, paint, roundPx, source.getWidth(), source.getHeight());
            }else if (type == TransType.TYPE_BOTTOM){
                clipTopLeft(canvas,paint,roundPx,source.getWidth(),source.getHeight());
                clipTopRight(canvas, paint, roundPx, source.getWidth(), source.getHeight());
            }else if (type == TransType.TYPE_TOP_LEFT){
                clipTopRight(canvas, paint, roundPx, source.getWidth(), source.getHeight());
                clipBottomLeft(canvas,paint,roundPx,source.getWidth(),source.getHeight());
                clipBottomRight(canvas,paint,roundPx,source.getWidth(),source.getHeight());
            }else if (type == TransType.TYPE_RIGHT){
                clipTopLeft(canvas,paint,roundPx,source.getWidth(),source.getHeight());
                clipBottomLeft(canvas,paint,roundPx,source.getWidth(),source.getHeight());
            }
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));


            //帖子图
            final Rect src = new Rect(0, 0, source.getWidth(), source.getHeight());
            final Rect dst = src;
            canvas.drawBitmap(result, src, dst, paint);
        }


        return result;
    }

    public String getId() {
        return getClass().getName() + Math.round(radius);
    }

    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {

    }

    private static void clipTopLeft(final Canvas canvas, final Paint paint, int offset, int width, int height) {
        final Rect block = new Rect(0, 0, offset, offset);
        canvas.drawRect(block, paint);
    }

    private static void clipTopRight(final Canvas canvas, final Paint paint, int offset, int width, int height) {
        final Rect block = new Rect(width - offset, 0, width, offset);
        canvas.drawRect(block, paint);
    }

    private static void clipBottomLeft(final Canvas canvas, final Paint paint, int offset, int width, int height) {
        final Rect block = new Rect(0, height - offset, offset, height);
        canvas.drawRect(block, paint);
    }

    private static void clipBottomRight(final Canvas canvas, final Paint paint, int offset, int width, int height) {
        final Rect block = new Rect(width - offset, height - offset, width, height);
        canvas.drawRect(block, paint);
    }

}
